using SM;
using SS;
using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace TVSGPP.dir
{
	public class frmSelectProductUnit : Form
	{
		private IContainer components = null;
		private Button btnClose;
		private Button btnSelect;
		private Grid dgvList1;
		private DataGridViewTextBoxColumn DID;
		private DataGridViewTextBoxColumn Column2;
		private DataGridViewTextBoxColumn number;
		private Func objFunc = new Func();
		public int Pid = 0;
		public string unitName = "";
		public string unitNumber = "1";
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
			DataGridViewCellStyle dataGridViewCellStyle = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();
			DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle();
			ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof(frmSelectProductUnit));
			this.btnClose = new Button();
			this.btnSelect = new Button();
			this.dgvList1 = new Grid();
			this.DID = new DataGridViewTextBoxColumn();
			this.Column2 = new DataGridViewTextBoxColumn();
			this.number = new DataGridViewTextBoxColumn();
			((ISupportInitialize)this.dgvList1).BeginInit();
			base.SuspendLayout();
			this.btnClose.BackColor = SystemColors.ButtonFace;
			this.btnClose.FlatAppearance.BorderColor = Color.FromArgb(192, 192, 255);
			this.btnClose.FlatStyle = FlatStyle.Flat;
			this.btnClose.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnClose.Location = new Point(152, 115);
			this.btnClose.Name = "btnClose";
			this.btnClose.Size = new Size(95, 26);
			this.btnClose.TabIndex = 6;
			this.btnClose.Text = "Thoát  [ ESC ]";
			this.btnClose.UseVisualStyleBackColor = false;
			this.btnClose.Click += new EventHandler(this.button1_Click);
			this.btnSelect.BackColor = SystemColors.ButtonFace;
			this.btnSelect.FlatAppearance.BorderColor = Color.FromArgb(192, 192, 255);
			this.btnSelect.FlatStyle = FlatStyle.Flat;
			this.btnSelect.Font = new Font("Arial", 9f, FontStyle.Bold, GraphicsUnit.Point, 163);
			this.btnSelect.Location = new Point(2, 115);
			this.btnSelect.Name = "btnSelect";
			this.btnSelect.Size = new Size(148, 26);
			this.btnSelect.TabIndex = 8;
			this.btnSelect.Text = "Chọn đơn vị tính [ F2 ]";
			this.btnSelect.UseVisualStyleBackColor = false;
			this.btnSelect.Click += new EventHandler(this.btnSelect_Click);
			this.dgvList1.AllowUserToAddRows = false;
			this.dgvList1.AllowUserToDeleteRows = false;
			this.dgvList1.AllowUserToResizeColumns = false;
			this.dgvList1.AllowUserToResizeRows = false;
			dataGridViewCellStyle.BackColor = Color.FromArgb(192, 255, 255);
			dataGridViewCellStyle.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvList1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle;
			this.dgvList1.BackgroundColor = Color.White;
			this.dgvList1.BorderStyle = BorderStyle.Fixed3D;
			this.dgvList1.ColumnHeadersHeight = 22;
			this.dgvList1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
			this.dgvList1.Columns.AddRange(new DataGridViewColumn[]
			{
				this.DID,
				this.Column2,
				this.number
			});
			this.dgvList1.EnableHeadersVisualStyles = false;
			this.dgvList1.Location = new Point(2, 2);
			this.dgvList1.Name = "dgvList1";
			this.dgvList1.ReadOnly = true;
			this.dgvList1.RowHeadersVisible = false;
			dataGridViewCellStyle2.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvList1.RowsDefaultCellStyle = dataGridViewCellStyle2;
			this.dgvList1.RowTemplate.DefaultCellStyle.Font = new Font("Arial", 9.75f, FontStyle.Regular, GraphicsUnit.Point, 0);
			this.dgvList1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.dgvList1.Size = new Size(245, 111);
			this.dgvList1.TabIndex = 0;
			this.dgvList1.CellDoubleClick += new DataGridViewCellEventHandler(this.dgvList1_CellDoubleClick);
			this.dgvList1.CellEnter += new DataGridViewCellEventHandler(this.dgvList1_CellEnter);
			this.DID.DataPropertyName = "UnitID";
			this.DID.HeaderText = "DID";
			this.DID.Name = "DID";
			this.DID.ReadOnly = true;
			this.DID.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.DID.Visible = false;
			this.Column2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.Column2.DataPropertyName = "UnitName";
			this.Column2.HeaderText = "Tên gọi";
			this.Column2.Name = "Column2";
			this.Column2.ReadOnly = true;
			this.Column2.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.number.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
			this.number.DataPropertyName = "number";
			dataGridViewCellStyle3.Alignment = DataGridViewContentAlignment.MiddleRight;
			this.number.DefaultCellStyle = dataGridViewCellStyle3;
			this.number.HeaderText = "Quy đổi";
			this.number.MinimumWidth = 80;
			this.number.Name = "number";
			this.number.ReadOnly = true;
			this.number.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.number.Width = 80;
			base.AutoScaleDimensions = new SizeF(7f, 15f);
			//base.AutoScaleMode = AutoScaleMode.Font;
			this.BackColor = Color.White;
			base.ClientSize = new Size(249, 143);
			base.ControlBox = false;
			base.Controls.Add(this.dgvList1);
			base.Controls.Add(this.btnSelect);
			base.Controls.Add(this.btnClose);
			this.Cursor = Cursors.Hand;
			this.Font = new Font("Arial", 9f, FontStyle.Regular, GraphicsUnit.Point, 0);
			//base.FormBorderStyle = FormBorderStyle.FixedToolWindow;
			//base.Icon = (Icon)componentResourceManager.GetObject("$this.Icon");
			base.KeyPreview = true;
			this.MaximumSize = new Size(251, 145);
			this.MinimumSize = new Size(251, 145);
			base.Name = "frmSelectProductUnit";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.Manual;
			base.Load += new EventHandler(this.frmProductDesease_Load);
			base.KeyDown += new KeyEventHandler(this.frmProductDesease_KeyDown);
			((ISupportInitialize)this.dgvList1).EndInit();
			base.ResumeLayout(false);
		}
		public frmSelectProductUnit()
		{
			this.InitializeComponent();
		}
		private void frmProductDesease_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}
		private void button1_Click(object sender, EventArgs e)
		{
			this.unitName = "";
			this.unitNumber = "1";
			base.Close();
		}
		private void frmProductDesease_Load(object sender, EventArgs e)
		{
			this.getListDesease1();
		}
		private void getListDesease1()
		{
			this.dgvList1.AutoGenerateColumns = false;
			this.dgvList1.DataSource = null;
			this.dgvList1.AutoGenerateColumns = false;
			SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
			string cmdText = "SELECT [uID] AS UnitID, [UnitName], [number] FROM [TabProductUnitList] WHERE [Pid] = " + this.Pid;
			SqlCommand selectCommand = new SqlCommand(cmdText, sqlConnection);
			SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);
			DataSet dataSet = new DataSet();
			sqlConnection.Open();
			sqlDataAdapter.Fill(dataSet);
			string text = "";
			string text2 = "";
			this.getRootUnit(ref text, ref text2);
			dataSet.Tables[0].Rows.Add(new object[]
			{
				text,
				text2,
				"1"
			});
			this.dgvList1.DataSource = dataSet.Tables[0];
			sqlConnection.Close();
			sqlConnection.Dispose();
			sqlDataAdapter.Dispose();
			dataSet.Dispose();
		}
		private void getRootUnit(ref string unitID, ref string unitName)
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
				string cmdText = "SELECT P_DonViTinh, UnitName FROM [TabProducts],[TabUnitProduct] WHERE [TabProducts].[P_DonViTinh] = [TabUnitProduct].[UnitID] AND [PID] = " + this.Pid.ToString();
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand(cmdText, sqlConnection);
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					unitID = sqlDataReader["P_DonViTinh"].ToString();
					unitName = sqlDataReader["UnitName"].ToString();
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
		public void AddDesease(int Pid, int Desease, string Name)
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand(string.Concat(new object[]
				{
					"INSERT INTO [TabProductUnitList] VALUES(",
					this.Pid,
					",",
					Desease,
					",N'",
					Name,
					"',0)"
				}), sqlConnection);
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		public void updateUnit(int Pid, int Desease, string number)
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand(string.Concat(new object[]
				{
					"UPDATE [TabProductUnitList] SET [number] = ",
					number,
					" WHERE [Pid] = ",
					Pid,
					" AND [uID] = ",
					Desease.ToString()
				}), sqlConnection);
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		public void DelDesease(int Pid, int Desease)
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(this.objFunc.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand(string.Concat(new object[]
				{
					"DELETE FROM [TabProductUnitList] WHERE [Pid] = ",
					Pid,
					" AND [uID] = ",
					Desease
				}), sqlConnection);
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		private void btnDel_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.dgvList1.RowCount; i++)
			{
				if (this.dgvList1.Rows[i].Cells[3].Value.ToString() == "True")
				{
					this.DelDesease(this.Pid, int.Parse(this.dgvList1.Rows[i].Cells[0].Value.ToString()));
				}
			}
			this.getListDesease1();
		}
		private void btnEdit_Click(object sender, EventArgs e)
		{
			this.dgvList1.ReadOnly = false;
		}
		private void btnSave_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.dgvList1.RowCount; i++)
			{
				this.updateUnit(this.Pid, int.Parse(this.dgvList1.Rows[i].Cells[0].Value.ToString()), this.dgvList1.Rows[i].Cells[2].Value.ToString());
			}
			this.dgvList1.ReadOnly = true;
		}
		private void dgvList1_CellEnter(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				this.unitName = this.dgvList1.Rows[e.RowIndex].Cells["Column2"].Value.ToString();
				this.unitNumber = this.dgvList1.Rows[e.RowIndex].Cells["number"].Value.ToString();
			}
			catch
			{
				this.unitName = "";
				this.unitNumber = "1";
			}
		}
		private void dgvList1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			this.btnSelect.PerformClick();
		}
		private void btnSelect_Click(object sender, EventArgs e)
		{
			base.Close();
		}
	}
}
